/*
 * Copyright (c) 2009 Johnny Mast
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */


#import "Vector3D.h"

Vector3D 
Vector3DMake(XFFloat x, XFFloat y, XFFloat z)
{
	Vector3D v = {x, y, z};
	return v;
}



Vector3D 
Add(Vector3D v1, Vector3D v2)
{
	return Vector3DMake(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z);
}



Vector3D 
Subtract(Vector3D v1, Vector3D v2)
{
	return Vector3DMake(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z);
}

Vector3D 
Vector3DInvert(Vector3D vector) {
	return Vector3DMake(-vector.x, -vector.y, -vector.z);	
}

Vector3D 
Vector3DScale(Vector3D vector, XFFloat scale)
{
	return Vector3DMake(vector.x * scale, vector.y * scale, vector.z * scale);
}


XFFloat 
DotProduct(Vector3D v1, Vector3D v2)
{
	return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
}



Vector3D 
CrossProduct(Vector3D v1, Vector3D v2)
{
	return Vector3DMake((v1.y * v2.z) - (v1.z * v2.y), (v1.x * v2.z) - (v1.z * v2.x), (v1.x * v2.y) - (v1.y * v2.x));
}



XFFloat 
Magnitude(Vector3D v)
{
	return sqrtf(v.x * v.x + v.y * v.y + v.z * v.z);
}



Vector3D Normalize(Vector3D v)
{
	float magnitude = sqrtf(v.x * v.x + v.y * v.y + v.z * v.z);
	return Vector3DMake(v.x / magnitude, v.y / magnitude, v.z / magnitude);
}